<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:252px;">
                    <div class="user-info">
                        <img src="../assets/img/img.jpg" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{ name }}</div>
                            <div>{{ role }}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        <span>欢迎您，{{adminName}}</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height:252px; text-align: center">
                    <div class="clearfix">
                        <span style="color: orange;">请牢记</span>
                    </div>
                    <hr/>
                    <div style="text-align: center; padding-top: 10px;" class="member">
                        <span>患者至上</span>
                        <br/>
                        <span>敬畏生命</span>
                        <br/>
                        <span>终身学习</span>
                        <br/>
                        <span>公平公正</span>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-user-solid grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{baseData.totalDoctor}}</div>
                                    <div><strong>医护人员总数</strong></div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-2">
                                <div class="grid-cont-right">
                                    <div class="grid-num" v-text="baseData.totalSales"></div>
                                    <div><strong>本月总销售金额</strong></div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-3">
                                <div class="grid-cont-right">
                                    <div class="grid-num" v-text="baseData.totalConsumes"></div>
                                    <div><strong>本月总支出金额</strong></div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height:403px;">
                    <schart style="padding-top: 30px" ref="bar" class="schart" canvasId="bar" :options="options" :key="updateKey"></schart>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import Schart from "vue-schart";
import { ref, reactive } from "vue";
import { getBaseData, getBarData } from '../api'
export default {
    name: "dashboard",
    components: { Schart },
    setup() {
        const name = localStorage.getItem("ms_username");
        const adminName = localStorage.getItem("adminName");
        const roleNum = localStorage.getItem("role");
        let role = '';
        if (roleNum === '1' || roleNum === 1) {
            role = "医生";
        } else if (roleNum === '2' || roleNum === 2) {
            role = "药品采购员";
        } else if (roleNum === '3' || roleNum === 3) {
            role = "药师";
        } else if (roleNum === '4' || roleNum === 4) {
            role = "仓管";
        } else {
            role = "超级管理员";
        }
        const baseData = ref({});
        const updateKey = ref(0);
        const options = {
            type: "bar",
            title: {
                text: "最近7天药品销售图",
            },
            xRorate: 25,
            labels: ['2021/01/12', '2021/01/13', '2021/01/14', '2021/01/15', '2021/01/16', '2021/01/17', '2021/01/18'],
            datasets: [
                {
                    label: "销售金额",
                    data: [0, 1, 2, 3, 4, 5 ,6]
                },
                {
                    label: "药品销量",
                    data: [0, 1, 2, 3, 4, 5, 6]
                }
            ],
        };

        const fetchBaseData = () => {
            getBaseData().then(res => {
                if(res.code === 200) {
                    baseData.value = res.data;
                }
            });
            getBarData().then(res => {
                if (res.code === 200) {
                    let d = res.data.message;
                    options.labels = d.map(item => {return item.date;});
                    options.datasets[0].data = d.map(item => {return item.price;});
                    options.datasets[1].data = d.map(item => {return item.quantity;});
                    updateKey.value += 1;
                    console.log(options);
                }
            })
        };
        fetchBaseData();

        return {
            name,
            adminName,
            baseData,
            options,
            role,
            updateKey
        };
    },
};
</script>

<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 20px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 24px;
    color: #999;
    line-height: 40px;
}

.user-info-list span {
    margin-left: 70px;
}

.member span {
    display: inline-block;
    padding-top: 8px;
    padding-bottom: 8px;
}
.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
